package com.crk.yiyuanguahao.service.impl;


import com.crk.yiyuanguahao.dao.FunctionDao;
import com.crk.yiyuanguahao.entity.Function;
import com.crk.yiyuanguahao.entity.User;
import com.crk.yiyuanguahao.service.FunctionService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: 程荣凯
 * @Date: 2019/3/21 16:27
 */
@Service
public class FunctionServiceImpl implements FunctionService {
    @Autowired
    FunctionDao functionDao;

    /**
     * 保存功能
     *
     * @param function
     * @return
     */
    @Override
    public boolean saveFunction(Function function) {
        int result = 0;
        if (null == function.getId()){
            function.setCreateTime(new Date());
            function.setUpdateTime(new Date());
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            function.setCreator(user.getId());
            function.setUpdator(user.getId());
            function.setStatus((short) 1);
            result = functionDao.insert(function);
            if (result<=0){
                return false;
            }
            Integer parentFunction = function.getParentFunction();
            Function parent = functionDao.selectByPrimaryKey(parentFunction);
            result = functionDao.updateByPrimaryKey(function);
        }else{
            Integer parentFunction = -1;
            if (null !=  function.getParentFunction()){
                parentFunction = function.getParentFunction();
            }
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            function.setUpdator(user.getId());
            function.setUpdateTime(new Date());
            result = functionDao.updateByPrimaryKey(function);
        }

        if (result>0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 删除功能
     *
     * @param functionId
     * @return
     */
    @Override
    public boolean deleteFunction(int functionId) {
        int result = functionDao.deleteByPrimaryKey(functionId);
        if (result>0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 根据功能ID查询功能
     *
     * @param functionId
     * @return
     */
    @Override
    public Function getFunctionById(int functionId) {
        Function one = functionDao.selectByPrimaryKey(functionId);
        return one;
    }

    /**
     * 根据角色查询功能列表
     *
     * @param roleId 角色ID
     * @param type   类型
     * @return
     */
    @Override
    public List<Function> getFunctionListByRoleAndType(int roleId, String type) {
        return null;
    }

    /**
     * 根据功能名称和类型查询列表
     *
     * @param functionName 功能名称
     * @param type         类型
     * @return
     */
    @Override
    public List<Function> queryFunctionByFunctionNameAndType(String functionName, Integer type) {
        return functionDao.selectAllByCondition(functionName,type);
    }

}
